# coding=utf-8
__author__ = 'liubin'

import os
from flask import url_for
from flask.ext.admin import form
from flask.ext.restful import Resource, fields, marshal
from project import app, db, Store, api_view, admin_view
from project.admin import ImageView

class StoreListApi(Resource):
    """
    restful访问方式
    需要定义可以解析的结构
    """

    # def _get_item_marshal(self):
    #     """
    #     Item 解析用结构
    #     :return:
    #     """
    #     item_fields = {}
    #     item_fields['id'] = fields.String
    #     item_fields['name'] = fields.String
    #     item_fields['description'] = fields.String
    #     item_fields['type'] = fields.String(default='item')
    #     item_fields['true_id'] = fields.Integer
    #     item_fields['price'] = fields.Price(decimals=2, default=0)
    #     item_fields['image'] = fields.String
    #     item_fields['leaf'] = fields.Boolean(default=True)
    #     return item_fields
    #
    # def _get_store_marshal(self):
    #     """
    #     Store解析用结构
    #     :return:
    #     """
    #     store_fields = {}
    #     store_fields['id'] = fields.String
    #     store_fields['name'] = fields.String
    #     store_fields['description'] = fields.String
    #     store_fields['type'] = fields.String(default='store')
    #     store_fields['true_id'] = fields.Integer
    #     store_fields['price'] = fields.Price(decimals=2, default=0)
    #     store_fields['image'] = fields.String
    #     store_fields['children'] = fields.Nested(self._get_item_marshal())
    #     return store_fields
    #
    # def _get_marshal(self):
    #     """
    #     最终结构
    #     children 为前台解析根节点必备
    #     :return:
    #     """
    #     base_fields = {
    #         'children': fields.Nested(self._get_store_marshal())
    #     }
    #     return base_fields

    # def get(self):
    #     stores = Store.query.all()
    #     data = {
    #         'children': []
    #     }
    #     for store in stores:
    #         store_image = os.path.join(app.config['UPLOAD_DIRECTORY'], store.image).replace('\\', '/')
    #         data_store = {
    #             'id': 'store-' + str(store.id),
    #             'name': store.name,
    #             'description': store.description,
    #             'true_id': store.id,
    #             'image': url_for('static', filename=form.thumbgen_filename(store_image)),
    #             'children': []
    #         }
    #         for item in store.items:
    #             item_image = os.path.join(app.config['UPLOAD_DIRECTORY'], item.image).replace('\\', '/')
    #             data_item = {
    #                 'id': 'item-' + str(item.id),
    #                 'name': item.name,
    #                 'description': item.description,
    #                 'true_id': item.id,
    #                 'price': item.price,
    #                 'image': url_for('static', filename=form.thumbgen_filename(item_image)),
    #                 'children': []
    #             }
    #             data_store['children'].append(data_item)
    #         data['children'].append(data_store)
    #     return marshal(data, self._get_marshal())
    _resource_fields = {
        'id': fields.String,
        'name': fields.String,
        'description': fields.String,
        'image': fields.String
    }

    def get(self, **kwargs):
        data = []
        for store in Store.query.all():
            store_image = os.path.join(app.config['UPLOAD_DIRECTORY'], store.image).replace('\\', '/')
            data_store = {
                'id': store.id,
                'name': store.name,
                'description': store.description,
                'image': url_for('static', filename=form.thumbgen_filename(store_image))
            }
            data.append(marshal(data_store, self._resource_fields))
        return {'success': True,  'data': data}

# 映射restful的url
api_view.add_resource(StoreListApi, '/api/store')
# Store Admin视图
admin_view.add_view(ImageView(Store, db.session))